//###############
// FILE : FileReaderIterator.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION: An iterator for files collection.
//###############
package oop.ex3.fileSearchModel.util;
import java.io.*;

import oop.ex3.fileSearchModel.util.exceptions.*;
/**
 * this class represents an iterator for a file collection
 * @author liorz06
 * @author ohad_dan
 */
public class FileReaderIterator implements FileIterator {
	//***********************************************************************
	//  Data members :                                                      *
	//***********************************************************************
	private BufferedReader _reader;
    private String _nextLine;
    //***********************************************************************
	//  Methods :                                                           *
	//***********************************************************************
   /**
    * Constructs a new file iterator.
    * @param reader File reader to iterate.
    * @throws ReaderIOException In case of I/O failure.
    */
    public FileReaderIterator(BufferedReader reader) 
    throws ReaderIOException{
        _reader = reader;
        try{ 
            _nextLine = _reader.readLine();
        }
        catch ( IOException e ) {
            throw new ReaderIOException("Error:I/O failure");
        }
    }
    /**
     * Checks if a next element exists.
     * @return True if there is next element, false otherwise.
     */
    public boolean hasNext() {
        return (_nextLine != null);
    }
    /**
	 * Returns next element.
	 * @return Next element.
	 * @throws ReaderIOException In case of I/O failure.
	 */
    public String next() throws ReaderIOException {
        String line = _nextLine;
        
        try {
            _nextLine=_reader.readLine();
        } catch (IOException e) {
            throw new ReaderIOException("Error:I/O failure");
        }
        return line;
    }
    
}
